<?php
class IndexController extends Zend_Controller_Action {
	public function init() {
		/* Initialize action controller here */
	}
	public function indexAction() {
		$pageNum = $this->getParam ( "page" );
		
		// get banners
		$bannerDb = new Application_Model_DbTable_Banner();
		$where = $bannerDb->getAdapter ()->quoteInto ( "del_flag = ?", 0 );
		$banners = $bannerDb->fetchAll ( $where );
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$select = $phoneDb->select();
		$select->from(array('p' => 'phone'))
			   ->joinInner(array('c'=> 'catagory'),'p.catagory_id = c.id' ,array())
			   ->where('c.del_flag = 0')
			   ->where('p.del_flag=0');
		$phones = $phoneDb->fetchAll ( $select );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 6 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		// get all catagories
		$catagoryDb = new Application_Model_DbTable_Catagory ();
		$catagories = $catagoryDb->fetchAll ( $where );
		
		$this->view->banners = $banners;
		$this->view->paginator = $paginator;
		$this->view->catagories = $catagories;
	}
	public function catagoryAction() {
		// get parameters
		$pageNum = $this->getParam ( "page" );
		$catagoryId = $this->getParam ( "id" );
		
		// get banners
		$bannerDb = new Application_Model_DbTable_Banner ();
		$where = $bannerDb->getAdapter ()->quoteInto ( "del_flag = ?", 0 );
		$banners = $bannerDb->fetchAll ( $where );
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$wherePhone = $phoneDb->getAdapter ()->quoteInto ( "catagory_id=? AND del_flag = 0", $catagoryId );
		$phones = $phoneDb->fetchAll ( $wherePhone );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 6 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		// get all catagories
		$catagoryDb = new Application_Model_DbTable_Catagory ();
		$catagories = $catagoryDb->fetchAll ( $where );
		
		// get current catagory
		$whereCatagory = $catagoryDb->getAdapter()->quoteInto("id=?", $catagoryId);
		$currentCatagory = $catagoryDb->fetchRow($whereCatagory);
		
		$this->view->banners = $banners;
		$this->view->paginator = $paginator;
		$this->view->catagories = $catagories;
		$this->view->currentCatagory = $currentCatagory;
	}
	
	public function feedbackAction() {
		$errors = array();
		if ($this->getRequest()->isPost()) {
			//name
			if (! Zend_Validate::is ( $this->getParam('name'), 'NotEmpty' )) {
				$errors [] = "'Tên' không được để trống.";
			}
				
			//email
			if (! Zend_Validate::is ( $this->getParam('email'), 'NotEmpty' )) {
				$errors [] = "'Email' không được để trống.";
			}elseif (! Zend_Validate::is ( $this->getParam('email'), 'EmailAddress' )) {
				$errors [] = "'Email' không hợp lệ.";
			}
				
			//title
			if (! Zend_Validate::is ( $this->getParam('title'), 'NotEmpty' )) {
				$errors [] = "'Tiêu đề' không được để trống.";
			}
				
			//content
			if (! Zend_Validate::is ( $this->getParam('content'), 'NotEmpty' )) {
				$errors [] = "'content' không được để trống.";
			}
				
			
				
			if (count($errors) == 0) {
				$this->view->success = true;
				$data = array(
						'name'		=> $this->getParam('name'),
						'email'		=> $this->getParam('email'),
						'title'		=> $this->getParam('title'),
						'content'		=> $this->getParam('content')
				);
				$feedbackDb = new Application_Model_DbTable_Feedback();
				$feedbackDb->insert($data);
				$this->redirect('/');
			}
			$this->view->name = $this->getParam('name');
			$this->view->email = $this->getParam('email');
			$this->view->title = $this->getParam('name');
			$this->view->content = $this->getParam('content');
				
		}
		$this->view->errors = $errors;
	}
}

